﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HSDc.RingleFlow.Api.Service;
using HSDc.RingleFlow.Api.Kernel;
using HSDc.RingleFlow.Kernel.ProcessActivity;

namespace HSDc.RingleFlow.Kernel.Default
{
    public class TaskServiceImpl: ITaskService
    {
        List<ITask> ITaskService.GetTasks(string UserID)
        {
            return GetTasks(UserID);
        }

        ITask ITaskService.GetTask(string UserID, string ProcessInstanceID)
        {
            return GetTask(UserID, ProcessInstanceID);
        }

        ITask ITaskService.GetTask(string TaskID)
        {
            ProcessInstance instance = null;
            ActivityInstance tmpTask = null;
            foreach (ProcessInstance instance2 in MemoryRuntimeEnvironment.instances.Values)
            {
                tmpTask = instance2.FindActivity(TaskID);
                if (tmpTask != null)
                    return tmpTask;
            }
            if (tmpTask == null)
                CreateNewProcessInstance(ref instance);
            string id2 = Guid.NewGuid().ToString();
            return instance.GetCurrentTask(string.Empty);
        }

        void ITaskService.CompleteTask(ITask task)
        {
            ((ActivityInstance)task).Complete();
        }

        void ITaskService.SaveTask(ITask task)
        {
        }

        private List<ITask> GetTasks(string UserID)
        {
            ProcessInstance instance = null;
            CreateNewProcessInstance(ref instance);
            List<ITask> tasks = new List<ITask>();
            ActivityInstance act = instance.GetCurrentTask(string.Empty);
            ((ITask)act).ChangeAssignee(UserID);
            tasks.Add(act);
            CreateNewProcessInstance2(ref instance);
            act = instance.GetCurrentTask(string.Empty);
            ((ITask)act).ChangeAssignee(UserID);
            tasks.Add(act);
            return tasks;
        }

        private ITask GetTask(string UserID, string ProcessInstanceID)
        {
            ProcessInstance instance = null;
            MemoryRuntimeEnvironment.instances.TryGetValue(ProcessInstanceID, out instance);
            if (instance == null)
                CreateNewProcessInstance(ref instance);
            string id = Guid.NewGuid().ToString();
            string id2 = Guid.NewGuid().ToString();
            //ActivityInstance act = instance.
            ActivityInstance act = instance.GetCurrentTask(string.Empty);
            ((ITask)act).ChangeAssignee(UserID);
            return act;
        }

        private void CreateNewProcessInstance(ref ProcessInstance instance)
        {
            string _id = Guid.NewGuid().ToString();
            DefaultProcessDefinition pd = new DefaultProcessDefinition();
            List<string> values = pd.GetValueByID("{DDC218B8-ED80-4F2F-BA96-EA6F9CF22729}");
            ProcessDefinition _pd = new ProcessDefinition(values[0], values[1], "1", true, 20);
            InitialDefaultActivities(ref _pd);
            instance = new ProcessInstance(_id, (ProcessDefinition)_pd);
            MemoryRuntimeEnvironment.instances.Add(_id, instance);
        }

        private void CreateNewProcessInstance2(ref ProcessInstance instance)
        {
            string _id = Guid.NewGuid().ToString();
            DefaultProcessDefinition pd = new DefaultProcessDefinition();
            List<string> values = pd.GetValueByID("{62DEA29E-CAB3-436C-B028-F3ECF1D7DCFB}");
            ProcessDefinition _pd = new ProcessDefinition(values[0], values[1], "1", true, 20);
            InitialDefaultActivities(ref _pd);
            instance = new ProcessInstance(_id, (ProcessDefinition)_pd);
            MemoryRuntimeEnvironment.instances.Add(_id, instance);
        }

        private void InitialDefaultActivities(ref ProcessDefinition pd)
        {
            Activity ac, ac2 = null;
            string _id = Guid.NewGuid().ToString();
            ac = new HumanActivity(_id, "主管審核", null, null);
            pd.AddActivity(ac);
            string _id2 = Guid.NewGuid().ToString();
            ac2 = new EndActivity(_id2, "End", null, null);
            Transition trans = new Transition(ac, ac2, null);
            ac.AddOutboundTransition(trans);
            pd.AddActivity(ac);
        }
    }
}
